﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Data;
using System.Data.Objects;
using System.Xml.Serialization;
using System.Linq.Expressions;
using EFEntities;
using ADONETDataServicesExtension.BLLUtils;

namespace BLL
{
    [EFEntityType(TypeName = "EFEntities.Employee")]
    public abstract class EmployeeBase : Contact
    {
        public EmployeeBase()
        {
        }
        public EmployeeBase(EFEntities.Employee employee)
            : base(employee)
        {
            EFEmployee = employee;
        }
        protected EmployeeBase(EmployeeBase employee)
            : base(employee.EFEmployee)
        {
            EFEmployee = employee.EFEmployee;
        }

        private EFEntities.Employee _efEmployee;
        protected EFEntities.Employee EFEmployee
        {
            get
            {
                if (_efEmployee == null)
                    _efEmployee = new EFEntities.Employee();
                return _efEmployee;
            }
            private set
            {
                _efEmployee = value;
                EFEntity = value;
            }
        }

        protected override EFEntities.Contact GetNewEFContact()
        {
            if (_efEmployee != null)
                return _efEmployee;
            _efEmployee = new EFEntities.Employee();
            return _efEmployee;
        }
        
        protected static EFEntities.Employee GetEFEmployee(EmployeeBase employee)
        {
            return employee.EFEmployee;
        }

        [EFEntityProperty]
        public DateTime Since
        {
            get { return EFEmployee.Since; }
            set { EFEmployee.Since = value; }
        }

        //private Manager _manager;
        //[EFEntityProperty]
        //public Manager Manager
        //{
        //    get
        //    {
        //        if (_manager == null)
        //            if (EFEmployee.Manager != null)
        //                _manager = new Manager(EFEmployee.Manager);
        //        return _manager;
        //    }
        //    set
        //    {
        //        _manager = value;
        //        if (value != null)
        //            EFEmployee.SetManagerReference(value.Id);
        //    }
        //}

        //private Team _team;
        //[EFEntityProperty]
        //public Team Team
        //{
        //    get
        //    {
        //        if (_team == null)
        //            if (EFEmployee.Team != null)
        //                _team = new Team(EFEmployee.Team);
        //        return _team;
        //    }
        //    set
        //    {
        //        _team = value;
        //        if (value != null)
        //            EFEmployee.SetTeamReference(value.Id);
        //    }
        //}

        public bool IsManager
        {
            get
            {
                EFEmployee.SubEmployees.TryLoadIfNotLoaded();
                return IsManagerExpression().Compile()(EFEmployee);
            }
        }
        public bool IsTeamManager
        {
            get
            {
                EFEmployee.TeamsManaged.TryLoadIfNotLoaded();
                return IsTeamManagerExpression().Compile()(EFEmployee);
            }
        }
        internal static Expression<Func<EFEntities.Employee, bool>> IsManagerExpression()
        {
            return e => e.SubEmployees.Any();
        }
        internal static Expression<Func<EFEntities.Employee, bool>> IsTeamManagerExpression()
        {
            return e => e.TeamsManaged.Any();
        }
    }
}
